本想写个Finder插件,鼠标右键弹出tinypng压缩菜单,直接点击压缩,在App程序上执行没问题,但Finder扩展上报错了,查了很多资料发现这是苹果的一个bug,哪怕开了沙盒,扩展也无法读写本地文件,几年了也没修复,所以放弃这种做法,脚本还是能用的。
https://tinypng.com/
1、tinify安装
1
| pip install --upgrade tinify
|
2、将脚本与压缩图片放在同一目录执行脚本
1 2
| cd xxxx/tinypng.py ./tinypng.py
|
tinypng.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
import sys import tinify import os import os.path
tinify.key = "92xubC2QKkKYUoeaD4yax-gAbER06fbh"
currentDir = os.getcwd() toFilePath = currentDir+"/tinyPng" print "currentDir=%s" %currentDir print "toFilePath=%s" %toFilePath
supportImgType = ['.jpg','.png'];
for item in os.listdir(currentDir): itemPath=os.path.join(currentDir,item) if os.path.isfile(itemPath): print "文件名=="+item fileName, fileSuffix = os.path.splitext(item) if fileSuffix in supportImgType: if os.path.isdir(toFilePath): pass else: os.mkdir(toFilePath) print('Doing:'+item) toFullName = toFilePath + '/' + item source = tinify.from_file(itemPath) source.to_file(toFullName) print('Finnish:'+item)
|